package com.weddingee.action.home;

import java.util.List;

import com.weddingee.action.base.BaseAction;
import com.weddingee.dao.home.PhotoDao;
import com.weddingee.dao.home.PhotoGroupDao;
import com.weddingee.dao.user.UserDao;
import com.weddingee.dto.ViewDto;
import com.weddingee.model.Photo;
import com.weddingee.model.PhotoGroup;
import com.weddingee.model.User;
import com.weddingee.util.DaoFactory;

public class PhotoAction extends BaseAction {

	private PhotoDao photoDao = (PhotoDao) DaoFactory.create(4);
	private PhotoGroupDao photoGroupDao = (PhotoGroupDao) DaoFactory.create(5);
	private UserDao userDao = (UserDao)DaoFactory.create(6);
	private Long id;
	private Long groupId;
	private String name;
	private String url;
	private List<Photo> photoList;
	private Photo photo;
	private ViewDto view = new ViewDto();
	private User user;
	private String type;
	
	public List<Photo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<Photo> photoList) {
		this.photoList = photoList;
	}

	// add
	public String add() {
		Photo photo = new Photo();
		//name = "21s是";
		//url = "asdsad";
		//groupId = 2L;
		photo.setGroupId(groupId);
		photo.setName(name);
		photo.setUrl(url);
		photoDao.save(photo);
		return SUCCESS;
	}


	// list
	public String list() {
		//groupId = 2L;
		photoList = photoDao.listPhoto(groupId);
		return SUCCESS;
	}

	// update
	public String update() {
		//id = 3L;
		//name = "哇撒飒飒";
		//url = "srscsasadsdsad";
		Photo photo = photoDao.find(id);
		if (name != null) {
			photo.setName(name);
		}
		if (url != null) {
			photo.setUrl(url);
		}

		photoDao.update(photo);
		return SUCCESS;
	}

	// delete
	public String delete() {
		//id = 4L;
		photoDao.delete(id);
		if( null == groupId ){
			log.info("参数错误。");
			return ERROR;
		}
		PhotoGroup pg = photoGroupDao.find(groupId);
		
		Long uid = pg.getUserId();
		if( null == uid){
			log.error("相册主人不存在。");
			return ERROR;
		}
		
		user = userDao.findByID(uid);
		type = user.getType();
		if( null == user ){
			log.error("用户不存在。");
			return ERROR;
		}
		
		photoList = photoDao.listPhoto(groupId);
		return SUCCESS;
	}

	// find
	public String find() {
		photo = photoDao.find(id);
		view.setData(photo);
		return SUCCESS;
	}

	public String show(){
		if( null == groupId ){
			log.info("参数错误。");
			return ERROR;
		}
		PhotoGroup pg = photoGroupDao.find(groupId);
		
		Long uid = pg.getUserId();
		if( null == uid){
			log.error("相册主人不存在。");
			return ERROR;
		}
		
		user = userDao.findByID(uid);
		type = user.getType();
		if( null == user ){
			log.error("用户不存在。");
			return ERROR;
		}
		
		photoList = photoDao.listPhoto(groupId);
		return SUCCESS;
	}
	
	public PhotoDao getPhotoDao() {
		return photoDao;
	}

	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public ViewDto getView() {
		return view;
	}

	public User getUser() {
		return user;
	}

	public String getType() {
		return type;
	}

	
}
